package com.liwenfeng.tcpandupd;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    public static void main(String[] args) {
        System.out.println("支持单客户端的服务端启动了，等待接收数据...");
        try(
                //1.创建ServerSockert对象，同时注册端口号。
                ServerSocket socket = new ServerSocket(8888);
                //2.调用accept方法，等待客户端的连接请求
                Socket acceptSocket = socket.accept();
                //3.从socket通信管道得到一个字节输入流
                InputStream is = acceptSocket.getInputStream();
                //4.把原始字节输入流包装成数据输入流
                DataInputStream dis = new DataInputStream(is);
        ) {
            String rs = null;
            do {
                try {
                    //5.读取信息
                    rs = dis.readUTF();
                } catch (IOException e) {
                    System.out.println(acceptSocket.getRemoteSocketAddress()+"离线了...");
                    break;
                }
                System.out.println(rs);
            } while (!rs.equals("quit"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
